﻿Module Globals
    Public configFile As String = "CauHinhCSDL"
    Public db As DbConnect
    Public config As DataTable
    Public Sub CreateConfigTable()
        config = New DataTable(configFile)
        config.Columns.Add("TenMayChu", GetType(String))
        config.Columns.Add("TenCSDL", GetType(String))
        config.Columns.Add("XacThuc", GetType(Integer))
        config.Columns.Add("TenNguoiDung", GetType(String))
        config.Columns.Add("MatKhau", GetType(String))
    End Sub
    Public Function base64Encode(ByVal data As String) As String
        Try
            Dim encData_byte(data.Length - 1) As Byte
            encData_byte = System.Text.Encoding.UTF8.GetBytes(data)
            Dim encodedData As String = Convert.ToBase64String(encData_byte)
            Return encodedData
        Catch e As Exception
            Throw New Exception("Error in base64Encode" & e.Message)
        End Try
    End Function
    Public Function base64Decode(ByVal data As String) As String
        Try
            Dim encoder As New System.Text.UTF8Encoding()
            Dim utf8Decode As System.Text.Decoder = encoder.GetDecoder()

            Dim todecode_byte() As Byte = Convert.FromBase64String(data)
            Dim charCount As Integer = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length)
            Dim decoded_char(charCount - 1) As Char
            utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0)
            Dim result As New String(decoded_char)
            Return result
        Catch e As Exception
            Throw New Exception("Error in base64Decode" & e.Message)
        End Try
    End Function
    Public Function EscapeString(ByVal str As String) 
        Return str.Replace("'", "''")
    End Function
    Public Sub OpenChildForm(ByVal parent As Form, ByVal child As Form)
        For Each f As Form In parent.MdiChildren
            If f.Name = child.Name Then
                f.Activate()
                Exit Sub
            End If
        Next
        child.MdiParent = parent
        child.Show()
    End Sub
    Public Sub SetCbBox(ByVal cb As ComboBox, ByVal disp As String, ByVal value As String, ByVal dt As DataTable)
        cb.DisplayMember = disp
        cb.ValueMember = value
        cb.DataSource = dt
    End Sub
    Public Sub NumberOnly(ByVal sender As Object, ByVal e As KeyPressEventArgs)
        If Not (Char.IsNumber(e.KeyChar) Or Char.IsControl(e.KeyChar)) Then
            e.Handled=True
        End If
    End Sub
End Module
